
/**
 * 帖子回复相关函数
 * 
 * @author		布衣才子
 * @date		2012-10-10
 * @copyright	2012-2015 YeeYi.com 
 * @email		work.jerryliu@gmail.com
 * @QQ			394969553
 */

var httpCode = 0;
var uid = getLocalStorge('uid');
var picUploadHttp = "http://www.yeeyi.com/bbs/forum.php?mod=mobile&ac=upload&uid="+uid;
var apiHost = "http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile";
var upload_image_url = '';
var b64 = new Base64();

var lat; //经度
var lon; //纬度

/**
 * 网络请求中转函数
 * 
 * @param {Object} url  请求的网络地址
 * @param {Object} callback  回调函数
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 关闭窗口
 */
function closeWindow(){
	uexWindow.close(-1);
}

/**
 * 组装针对主贴回复的操作
 */
function getReplyTitle(){
	var title = getLocalStorge('title');
	var QThread = getLocalStorge('QThread');
	var tid = getLocalStorge('tid');
	
	if(!QThread || QThread != tid){
		if(title.length > 0){
			var titleContent = '<font color="red">RE: </font>'+title;
			$('#title').html(titleContent);
		}else{
			$('#title').html('回复主题');
		}
	}else{
		if(title.length > 0){
			var titleContent = '<font color="red">引用主贴: </font>'+title;
			$('#title').html(titleContent);
		}else{
			$('#title').html('引用主题');
		}
	}
	
	
}

/**
 * 组装针对回复内容的操作title
 */
function getSubReplyTitle(){
	var toReply = getLocalStorge('toReUsername');
	var rpid = getLocalStorge('rpid');
	var toUid = getLocalStorge('toReUid');
	var quote = getLocalStorge('quote');
	
	if(toReply && rpid && toUid){
		if(!quote || quote != rpid){
			title = '回复 '+toReply;
			$('#title').html(title);
		}else{
			title = '引用  "'+toReply+'" 的回复';
			$('#title').html(title);
		}
		
	}else{
		uexWindow.alert('参数错误','缺少必要参数，请重新操作！','返回');
		uexWindow.close(-1);
	}
}


/**
 * 选择或拍摄好照片后的响应函数
 * 
 * @param {Object} opCode
 * @param {Object} dataType
 * @param {Object} data
 */
function getReadyToUpload(opCode, dataType, data){
	upload_image_url = data;
	var thumbObj = $('.thumbPlace');
	var thumbHTML = '<div onclick="cancelPic(this)" class="docancel"><img src="css/images/cancel.png" class="cancelPic"><img class="thumbPic" src="'+data+'" /></div>';
	thumbObj.append(thumbHTML);
	$('.showpic').show();
	
	httpCode = httpCode + 1;
	showLoading("图片开始上传，请稍后……",0);
	uexUploaderMgr.createUploader(httpCode,picUploadHttp);
}

/**
 * 创建上传的管理对象
 * 
 * @param {Object} opCode
 * @param {Object} dataType
 * @param {Object} data
 */
function uexUploaderMgrCbCreateUploader(opCode,dataType,data){
	if(data == 0){
		uexUploaderMgr.uploadFile(opCode,upload_image_url,"Filedata","2");	
	}else{
        showLoading("上传失败……",1);
		removeErrorPic();
	}
}

/**
 * 图片上传过程的监控函数
 * 
 * @param {Object} opCode
 * @param {Object} fileSize
 * @param {Object} percent
 * @param {Object} serverReback
 * @param {Object} status
 */
function uexUploaderMgrStatusCb(opCode,fileSize,percent,serverReback,status){
	switch (status) {
           case 0://正在上传
                 if(fileSize>2560000){
                     uexUploaderMgr.closeUploader(opCode);
                     showLoading('上传失败！您上传的图片大小为：'+parseInt(fileSize/1000)+'KB，超过2M的限制！',1);
					 removeErrorPic();
					 return false;
                  }else{
                      showLoading('已上传'+percent+'%',0);
                  }
                  break;
           case 1://上传完毕
                  if(serverReback.length > 3){
				  	  //alert(serverReback);
					  //alert('1');
				  	  givePicAid(serverReback);
                      uexWindow.toast("0","5","图片上传成功…","2000");
					  showLoading('图片上传成功！',1);   
                  }else{
				  	  if(serverReback == '407'){
					  	 uexWindow.alert("未找到当前用户，请先登录!");
						 removeErrorPic();
					  }	
					  //alert(serverReback);
					  //alert('1');
                      uexWindow.toast("0","5","图片上传失败…","2000");
					  showLoading('图片上传失败！',1);
					  removeErrorPic();
                  }
                  
				  uexUploaderMgr.closeUploader(opCode);
                  break;
           case 2:
		   		 //alert(serverReback);
				 //alert('2');
                 uexWindow.alert("图片上传失败");
				 showLoading("图片上传失败……",1);
				 removeErrorPic();
                 uexUploaderMgr.closeUploader(opCode);
                 break;
        }    
}


/**
 * 针对主贴回复的操作函数
 */
function postNewReply(){
	uexWindow.toast("1","5","回复内容正在发布…","0");
	var imgObj = $(".thumbPic");
	var allAid = '';
	var imgClass = '';
	$.each(imgObj,function(index,name){
		imgClass = $(name).attr('title');
		if(allAid == ''){
			allAid = imgClass;
		}else{
			allAid = allAid + ',' + imgClass;
		}
	})
	var regionStr = $.trim($('#region').text());
	var message = $.trim($('#message').val());
	var fid = getLocalStorge('fid');
	var uid = getLocalStorge('uid');
	var username = getLocalStorge('username');
	var password = getLocalStorge('pwd');
	var tid = getLocalStorge('tid');
	
	
	if(message.length <= 1){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，内容太短了些吧…","2000");
		return false;
	}
	
	if(!fid){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，没有找到对应板块…","2000");
		return false;
	}
	
	if(!tid){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，帖子ID信息丢失…","2000");
		return false;
	}
	
	if(!uid){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，用户UID缺失…","2000");
		return false;
	}
	var pmessage;
	var QThread = getLocalStorge('QThread');
	if(!QThread || QThread != tid){
		pmessage = message;
		if(regionStr){
			pmessage = pmessage + "<br><br><img src='http://www.yeeyi.com/bbs/static/image/common/location.png'>我现在的位置："+regionStr;
		}
		var q = 'no';
	}else{
		pmessage = '<p style="background-color:#eee;padding-left:0.3em;">引用主题帖</p><br>'+message;
		if(regionStr){
			pmessage = pmessage + "<br><br><img src='http://www.yeeyi.com/bbs/static/image/common/location.png'>我现在的位置："+regionStr;
		}
		message = '引用主题帖<br><br>'+message;
		var q = 'yes';
	}
	
	localStorage.setItem('replyMessage',pmessage);
	
	_message = encodeURIComponent(b64.encode(message));
	_region = encodeURIComponent(b64.encode(regionStr));
	_imgaid = encodeURIComponent(allAid);
	_username = encodeURIComponent(b64.encode(username));
	_password = encodeURIComponent(password);
	
	var RequestUrl = apiHost + '&ac=reply&tid='+tid+'&message='+_message+'&fid='+fid+'&imgaid='+_imgaid+'&uid='+uid+'&username='+_username+'&password='+_password+'&region='+_region+'&q='+q;
	
	xmlHttp(RequestUrl,replyCallBack);
}

/**
 * 针对帖子回复内容的回复执行函数，向服务器执行请求
 */
function postSubReply(){
	uexWindow.toast("1","5","回复内容正在发布…","0");
	var imgObj = $(".thumbPic");
	var allAid = '';
	var imgClass = '';
	var pmessage;
	$.each(imgObj,function(index,name){
		imgClass = $(name).attr('title');
		if(allAid == ''){
			allAid = imgClass;
		}else{
			allAid = allAid + ',' + imgClass;
		}
	})
	
	var regionStr = $('#region').text();
	var message = $('#message').val();
	var fid = getLocalStorge('fid');
	var uid = getLocalStorge('uid');
	var username = getLocalStorge('username');
	var password = getLocalStorge('pwd');
	var tid = getLocalStorge('tid');
	var toReplyUsername = getLocalStorge('toReUsername');
	var toReplyUid = getLocalStorge('toReUid');
	
	var rpid = getLocalStorge('rpid');
	var quote = getLocalStorge('quote');
	
	if(message.length == 0){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，内容太短了些吧…","2000");
		return false;
	}
	
	if(!toReplyUid){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，参数丢失，请重试…","2000");
		return false;
	}
	
	if(!fid){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，没有找到对应板块…","2000");
		return false;
	}
	
	if(!tid){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，帖子ID信息丢失…","2000");
		return false;
	}
	
	if(!uid){
		uexWindow.closeToast();
		uexWindow.toast("0","5","提交失败，用户UID缺失…","2000");
		return false;
	}
	
	if(!quote || quote != rpid){
		pmessage = '<p style="background-color:#eee;padding-left:0.3em;"> 回复 '+toReplyUsername+' 的帖子</p><br>'+message;
		message = '回复 '+toReplyUsername+' 的帖子<br><br>'+message;
		if(regionStr){
			pmessage = pmessage + "<br><br><img src='http://www.yeeyi.com/bbs/static/image/common/location.png'>我现在的位置："+regionStr;
		}
		var q = 'no';
	}else{
		pmessage = '<p style="background-color:#eee;padding-left:0.3em;"> 引用 '+toReplyUsername+' 的帖子<p><br>'+message;
		if(regionStr){
			pmessage = pmessage + "<br><br><img src='http://www.yeeyi.com/bbs/static/image/common/location.png'>我现在的位置："+regionStr;
		}
		message = '引用 '+toReplyUsername+'的帖子<br><br>'+message;
		var q = 'yes';
	}
	
	localStorage.setItem('replyMessage',pmessage);
	_message = encodeURIComponent(b64.encode(message));
	_region = encodeURIComponent(b64.encode(regionStr));
	_imgaid = encodeURIComponent(allAid);
	_username = encodeURIComponent(b64.encode(username));
	_password = encodeURIComponent(password);
	
	var RequestUrl = apiHost + '&ac=reply&tid='+tid+'&message='+_message+'&fid='+fid+'&imgaid='+_imgaid+'&uid='+uid+'&username='+_username+'&password='+_password+'&region='+_region+'&touid='+toReplyUid+'&q='+q;
	//alert(RequestUrl);
	xmlHttp(RequestUrl,replyCallBack);
}

/**
 * 发布新回复的回调函数
 * @param {Object} data
 */
function replyCallBack(data){
	uexWindow.closeToast();
	var returnInfo = data.info;
	if(parseInt(returnInfo) > 10000){
		uexWindow.toast("0","5","回复成功,即将跳转到原帖…","2000");
		//将新回复内容追加到帖子尾部
		uexWindow.evaluatePopoverScript("threadWindow","content","appendNewSubReply("+returnInfo+")");
		setTimeout(function () {
			uexWindow.close(-1);
		}, 2000);
	}else if(returnInfo == '403'){
		uexWindow.toast("0","5","发布失败，用户名或密码错误…","2000");
	}else if(returnInfo == '405'){
		uexWindow.toast("0","5","发布失败，您的用户组无权发帖…","2000");
	}else if(returnInfo == '406'){
		uexWindow.toast("0","5","发布失败，缺少参数…","2000");
	}else{
		uexWindow.toast("0","5","发布失败，请稍后重试…","2000");
	}
}

/**
 * 打开地理位置获取API
 */
function getLocation(){
	var locationType = getLocalStorge('locationType');
	if(locationType == 'pressed'){
		removeRigion();
		localStorage.setItem('locationType','');
	}else{
		$('.regions img').attr('src','css/images/location_pressed.png');
		localStorage.setItem('locationType','pressed');
		uexLocation.openLocation()
	}
	
}

/**
 * 把论坛上的图片id赋值给对应图片的class，为内容提交做准备
 * @param {Object} aid 图片ID
 */
function givePicAid(aid){
	$(".thumbPic").last().attr('title',aid);
}

/**
 * 服务器上图片上传失败时，消去当前缩略图片
 */
function removeErrorPic(){
	$('.docancelPic').last().remove();
}

/**
 * 取消选中的图片
 * @param {Object} objclass
 */
function cancelPic(objclass){
	$(objclass).remove();
}

/**
 * 显示进度信息
 * @param {Object} obj
 * @param {Object} status
 */
function showLoading(obj,status){
	if(status == 1){
		$("#loadingPic").hide();
	}else{
		$("#loadingPic").show();
	}
	$('#loadingWord').html(obj);
	$('.uploading').show();
}

/**
 * appcan Error监听函数
 * @param {Object} opCode
 * @param {Object} errorCode
 * @param {Object} errorInfo
 */
function uexWidgetOneCbError(opCode, errorCode, errorInfo){
	alert("errorCode:" + errorCode + "\nerrorInfo:" + errorInfo);
	showLoading("上传失败……",1);
}

/**
 * 获取经纬度
 * @param {Object} inLatitude
 * @param {Object} inLongitude
 */
function locationRequestCallBack(inLatitude,inLongitude){
	uexWindow.toast("1","5","开始获取您的位置…","0");
	uexLocation.closeLocation();
	lat = inLatitude;
	lon = inLongitude;
	
	uexLocation.getAddress(lat,lon);
}

/**
 * 获取经纬度回调函数
 * @param {Object} opId
 * @param {Object} dataType
 * @param {Object} data
 */
function getAddressCallBack(opId,dataType,data){
	if(dataType == 0){
		$('#region').html(data);
		$('.region').show();
		showLoading('提示：再次点击坐标图片，可清除地址！',1);
		uexWindow.closeToast();
		uexWindow.toast("0","5","获取成功…","2000");
	}else{
		uexWindow.closeToast();
		uexWindow.toast("0","5","获取失败…","2000");
	}
}

/**
 * 取消经纬度信息
 */
function removeRigion(){
	$('#region').html('');
	$('.regions img').attr('src','css/images/region.png');
	$('.region').hide();
	$('.uploading').hide();
	$('#loadingPic').show();
	$('#loadingWord').text('');
}

/**
 * HTML5 localstorage.getItem 的封装版函数
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}
 